import {Component, OnInit, Input, Output, EventEmitter, AfterContentInit, AfterViewInit} from '@angular/core';
import {LocalStorage} from "../../app.local-storage";
import {UserModel} from "../../model/user.model";
import {CONSTANT} from "../../service/constant.service";
import {TitleCpt} from "../../lib/title/title.component";
import {USER} from "../../service/user.service";
import {CheckStandModel} from "../../model/checkstand.model";
import {ROUTE} from "../../service/route.service";
import {MeterService} from "../../service/meter.service";
import {CONTRACT} from "../../service/contract.service";

@Component({
  selector: 'app-payment-view',
  templateUrl: './payment-view.component.html',
  styleUrls: ['./payment-view.component.scss']
})
export class PaymentViewComponent implements OnInit,AfterViewInit {
  ngAfterViewInit():void {
    console.log("ngAfterViewInit");
  }

  @Input('showModal') public isModalShown: boolean = false;
  @Output('modalClose') public modalClose = new EventEmitter();
  hello: string;
  user: any = UserModel;
  paymentViewTitleCpt: Array<TitleCpt> = this.constant.genPaymentTitleCptList();
  data = {
    contractId: undefined,
    money: 50,
    type: 1
  };
  bill:any;
  moneyItems = [
    {name: '50元', value: 50},
    {name: '100元', value: 100},
    {name: '200元', value: 200}
  ];
  currentContract:any = {}

  constructor(private ls: LocalStorage,
              private USER: USER,
              private route: ROUTE,
              private contractApi:CONTRACT,
              public METER:MeterService,
              private constant: CONSTANT) {


  }

  public showModal(): void {
    this.isModalShown = true;
    this.USER.refreshContract.subscribe(v=> {
      console.log("USER.refreshContract",v);
      this.data.contractId = v.id;
      this.loadContractBill()
    })
    this.contractApi.getContractList(v=>{
      let contractList = []
      for(let item of v.content){
        if(item.status == 8){
          contractList.push(item) ;
        }
      }
      this.data.contractId= contractList[0].id
      this.currentContract.id =contractList[0].id
      this.currentContract.remainingDays= contractList[0].remainingDays
      this.USER.refreshC(this.currentContract);
    })
  }

  public onHidden(): void {
    this.isModalShown = false;
  }

  billTypeItems: any = [
    {id:1, name: '交房租', isActive: true},
    {id:2, name: '水费充值', isActive: false},
    {id:3, name: '电费充值', isActive: false}
  ];

  toggleType(item: any,) {
    for (let i = 0; i < this.billTypeItems.length; i++) {
      this.billTypeItems[i].isActive = false;
      item.isActive = true;
      this.data.type = item.id;
    }
  }

  ngOnInit() {
    if (this.ls.getObject('user')) {
      this.user = this.ls.getObject('user') as UserModel;
    }
    this.hello = this.constant.getNewDate();

  }
  doCharge(){
    console.log(this.data)
    this.METER.recharge(this.data, v=> {
      let checkstand = new CheckStandModel(v.uuid, v.content, v.money, v.id, 'personal/mylifepayment');
      this.ls.setObject('checkstand', checkstand);
      this.route.goConfirmPaymentView(v.uuid);
    })
  }
  loadContractBill() {
    let reqData:any = {
      contractId: this.data.contractId,
      type:1,
      status:1
    }
    this.contractApi.getBill(reqData, (res:any)=> {
      let list = res.content
      this.bill = res.content[0]
    })
  }
  payRent() {
    console.log(this.bill)
    if (this.bill) {
      let checkstand = new CheckStandModel(this.bill.uuid, this.bill.content, this.bill.money, this.bill.id, 'personal/myrent')
      this.ls.setObject('checkstand', checkstand);
      this.route.goConfirmPaymentView(this.bill.uuid);
    }
  }
  goPay(){
    if (this.data.type == 1){
      this.payRent()
    }else{
      this.doCharge()
    }
    this.onHidden()
  }
}
